<?php

class AddArticle extends HTML_QuickForm {
  private $tpl;
  private $art;

  function __construct($tpl,$art) {
    $this->tpl = $tpl;
    $this->art = $art;

    if($this->tpl->get_template_vars('modify')) {
    $img = $art->getWallpaper();
    $defaults = array('name' => $img['name'],
                     'alias' => $img['alias'],
                  'category' => $img['ctgy'],
                  'provider' => $img['provider'],
                    'status' => $img['status'],
                       'wap' => $img['wap']);
    } else {
      $defaults = array ('ihome' => 1,
                         'acomm' => 1,
                         'schedule' => 0,
                         'sdate' => array('d' => date('d'),
                                          'm' => date('m'),
                                          'H' => date('H'),
                                          'i' => date('i'))
                         );
    }

    $this->setDefaults($defaults);

    $sid = isset($_POST['sections']) ? $_POST['sections'] : 0;
    $rs = $art->getDb()->CacheGetAll('SELECT * FROM mod_sections WHERE lft > 0 ORDER BY root_id, lft ASC');
    $sections = ($sid > 0) ? '<select name="sections"><option value="">'._('--Select--').'</option>' :'<select name="sections"><option value="" selected="selected">'._('--Select--').'</option>';
    foreach ($rs as $sec) {
      if($sec['level'] == 0  AND $sec['id'] == 1) {
        $sections .= ($sid == $sec['id']) ? "<optgroup label=\"$sec[name]\"><option value=\"$sec[id]\" selected=\"selected\">$sec[name]</option>" : "<optgroup label=\"$sec[name]\"><option value=\"$sec[id]\">$sec[name]</option>";
      } elseif ($sec['level'] == 0) {
        $sections .= ($sid == $sec['id']) ? "</optgroup><optgroup label=\"$sec[name]\"><option value=\"$sec[id]\" selected=\"selected\">$sec[name]</option>" : "</optgroup><optgroup label=\"$sec[name]\"><option value=\"$sec[id]\">$sec[name]</option>";
      } else {
        $sections .= ($sid == $sec['id']) ? "<option value=\"$sec[id]\" selected=\"selected\">".indent($sec['name'], $sec['level'], '&nbsp;&nbsp;&nbsp;')."</option>" : "<option value=\"$sec[id]\">".indent($sec['name'], $sec['level'], '&nbsp;&nbsp;&nbsp;')."</option>";
      }
    }
    $sections .= '</select>';

    $this->HTML_QuickForm('addarticle');
    $this->addElement('header', 'header', 'addarticle');
    $this->tpl->assign('section',$sections);
    $this->addElement('static', 'sections', _('Sections'), '&nbsp');
    $this->addElement('text', 'title', _('title'), array('size' => 80, 'maxlength' => 255));
    $this->addElement('textarea', 'body', _('body'), array('rows' => 20, 'cols' => 80));
    $ihome[] =& $this->createElement('radio', null, null, _('yes'), '1', array('class' => 'radio'));
    $ihome[] =& $this->createElement('radio', null, null, _('no'), '0', array('class' => 'radio'));
    $this->addGroup($ihome, 'ihome', _('publish in home'), '&nbsp;');
    $acomm[] =& $this->createElement('radio', null, null, _('yes'), '1', array('class' => 'radio'));
    $acomm[] =& $this->createElement('radio', null, null, _('no'), '0', array('class' => 'radio'));
    $this->addGroup($acomm, 'acomm', _('allow comments'), '&nbsp;');
    $schedule[] =& $this->createElement('radio', null, null, _('yes'), '1', array('class' => 'radio'));
    $schedule[] =& $this->createElement('radio', null, null, _('no'), '0', array('class' => 'radio'));
    $this->addGroup($schedule, 'schedule', _('schedule article'), '&nbsp;');
    $this->addElement('date', 'sdate', _('schedule:'), array('format' => 'dmYHi', 'minYear' => date('Y'), 'maxYear' => date('Y') + 1));

    $this->addElement('submit', 'btn_Submit', _('Submit'), array('class' => 'submit'));
    $this->applyFilter('__ALL__', 'trim');

    $this->addRule('sections', _('section required'), 'required');
    $this->addRule('title', _('title required'), 'required');
    $this->addRule('body', _('body required'), 'required');
    $this->registerRule('sections', 'callback', 'checkSections', $this);
  }

  function displayForm() {
    $this->renderer = new HTML_QuickForm_Renderer_ArraySmarty($this->tpl);
    $this->renderer->setRequiredTemplate('{if $error}<font color="red">{$label}</font>{else}{$label}{if $required}&nbsp;<font color="red" size="1">*</font>{/if}{/if}');
    $this->renderer->setErrorTemplate('{if $error}<font color="blue" size="1">{$error}</font><br/>{/if}{$html}');
    $this->setRequiredNote('<small class="red">*&nbsp;</small><small class="blue">'._('required_fields').'</small>');
    $this->accept($this->renderer);
    $this->tpl->assign('form_data',$this->renderer->toArray());
    $this->tpl->assign('msg',$this->art->getMsg());
  }

  function process() {
    $this->tpl->assign('result', $this->art->addArticle($this->getSubmitValues()));
    $this->tpl->assign('msg',$this->art->getMsg());
  }

} // end of class

?>